<?php

include('loaddb.php');

$output=array();
if (isset($_GET['getchildren']))
{
	$children=array();
	if (is_array($_GET['getchildren']))
		$children=$_GET['getchildren'];
	else
		$children[]=$_GET['getchildren'];
	foreach ($children as $child)
	{
		if ($child=='')
		{	
			//get toplevel data
			$result=$mysql->query("select id,hostname from hosts order by lower(hostname)");
			while ($myrow=$result->fetch_array())
			{
				$output[$child]['children'][]=array('id'=>'host'.$myrow['id'], 'name'=>$myrow['hostname']);
			}
		}
		else if (substr($child,0,4)=='host')
		{
			$hostid=substr($child,4)*1;
			$result=$mysql->query("select id,pathname from paths where parentpathid is null and hostid=$hostid order by lower(pathname)");
			while ($myrow=$result->fetch_array())
			{
				$output[$child]['children'][]=array('id'=>'path'.$myrow['id'], 'name'=>basename($myrow['pathname']));
			}
		}
		else if (substr($child,0,4)=='path')
		{
			$pathid=substr($child,4)*1;
			$result=$mysql->query("select id,pathname from paths where parentpathid=$pathid order by lower(pathname)");
			while ($myrow=$result->fetch_array())
			{
				$output[$child]['children'][]=array('id'=>'path'.$myrow['id'], 'name'=>basename($myrow['pathname']));
			}
			
			$result=$mysql->query("select id,filename from files where pathid=$pathid order by lower(filename)");
			while ($myrow=$result->fetch_array())
			{
				$output[$child]['children'][]=array('id'=>'file'.$myrow['id'], 'name'=>$myrow['filename']);
			}
		}
	}
}


if (isset($_GET['getchildrenscandata']) && isset($_GET['scanid']))
{
	$children=array();
	if (is_array($_GET['getchildrenscandata']))
		$children=$_GET['getchildrenscandata'];
	else
		$children[]=$_GET['getchildrenscandata'];
	$scanid=$_GET['scanid']+0;
		
	foreach ($children as $child)
	{
		if ($child=='')
		{	
			//get toplevel data
			$result=$mysql->query("select id,hostname,numsharedfiles,totalsize from hosts,host_stats where hosts.id=host_stats.hostid and host_stats.scanid=$scanid order by lower(hostname)");
			while ($myrow=$result->fetch_array())
			{
				$output[$child]['children'][]=array('id'=>'host'.$myrow['id'], 'name'=>$myrow['hostname'], 'type'=>'host', 'totalsize'=>$myrow['totalsize'], 'sharedfiles'=>$myrow['numsharedfiles']);
			}
		}
		else if (substr($child,0,4)=='host')
		{
			$hostid=substr($child,4)*1;
			$result=$mysql->query("select id,pathname,numsharedfiles,totalsize from paths,path_stats where parentpathid is null and hostid=$hostid and paths.id=path_stats.pathid and path_stats.scanid=$scanid order by lower(pathname)");
			while ($myrow=$result->fetch_array())
			{
				$output[$child]['children'][]=array('id'=>'path'.$myrow['id'], 'name'=>basename($myrow['pathname']), 'type'=>'host', 'type'=>'folder', 'totalsize'=>$myrow['totalsize'], 'sharedfiles'=>$myrow['numsharedfiles']);
			}
		}
		else if (substr($child,0,4)=='path')
		{
			$pathid=substr($child,4)*1;
			$result=$mysql->query("select id,pathname,numsharedfiles,totalsize from paths,path_stats where parentpathid=$pathid and paths.id=path_stats.pathid and path_stats.scanid=$scanid order by lower(pathname)");
			while ($myrow=$result->fetch_array())
			{
				$output[$child]['children'][]=array('id'=>'path'.$myrow['id'], 'name'=>basename($myrow['pathname']), 'type'=>'folder', 'totalsize'=>$myrow['totalsize'], 'sharedfiles'=>$myrow['numsharedfiles']);
			}
			
			$result=$mysql->query("select id,filename,filesize from files,file_stats where pathid=$pathid and files.id=file_stats.fileid and file_stats.scanid=$scanid order by lower(filename)");
			while ($myrow=$result->fetch_array())
			{
				$output[$child]['children'][]=array('id'=>'file'.$myrow['id'], 'name'=>$myrow['filename'], 'type'=>'file', 'totalsize'=>$myrow['filesize']);
			}
		}
	}
}
		
if (isset($_GET['getdata']))
{
	$requested=array();
	if (is_array($_GET['getdata']))
		$requested=$_GET['getdata'];
	else
		$requested[]=$_GET['getdata'];
	foreach ($requested as $request)
	{
		if (substr($request,0,4)=='host')
		{
			$hostid=substr($request,4)*1;
			$result=$mysql->query("select unix_timestamp(time) as time,totalsize as height
				from scans 
				left join host_stats on (host_stats.scanid=scans.id and hostid=$hostid)
				order by time asc");
		}
		else if (substr($request,0,4)=='path')
		{
			$pathid=substr($request,4)*1;
			$result=$mysql->query("select unix_timestamp(time) as time,totalsize as height
				from scans 
				left join path_stats on (path_stats.scanid=scans.id and pathid=$pathid)
				order by time asc");
		}
		else if (substr($request,0,4)=='file')
		{
			$fileid=substr($request,4)*1;
			$result=$mysql->query("select unix_timestamp(time) as time,filesize as height
				from scans 
				left join file_stats on (file_stats.scanid=scans.id and fileid=$fileid)
				order by time asc");
		}
		while ($result && $myrow=$result->fetch_array())
		{
			$scantime=$myrow['time']*1;
			if ($myrow['height']==null)
				$myrow['height']=0;
			$size=$myrow['height']*1;
	#		$size=$myrow['numsharedfiles'];
	
			$output[$request]['data'][]=array('x'=>$scantime, 'height'=>$size);
		}
	}
}

if (isset($_GET['getnowtime']))
{
	$output['nowtime']=time();
}
if (isset($_GET['getlastscan']))
{
	$result=$mysql->query("SELECT value FROM int_settings WHERE name='curscan'");
	$myrow=$result->fetch_array();
	if ($myrow)
		$output["lastscan"]=$myrow['value'];
}
	
print json_encode($output);
